package com.jf3q.iot.aop;

import com.jf3q.iot.domain.IotOperationLog;
import com.jf3q.iot.mapper.IotOperationLogMapper;
import com.jf3q.iot.utils.InstructTypeConstant;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
@EnableAspectJAutoProxy
public class OperationLogAspect {

    @Resource
    IotOperationLogMapper operationLogMapper;

    @Pointcut("execution( * com.jf3q.iot.service.impl.SendInstructsServiceImpl.sendInstructs(..))")
    public void point(){
    }

    //后置增强
    @AfterReturning(value = "point()")
    public void afterReturning(JoinPoint jp){
        Object [] args = jp.getArgs();
        //取出参数
        String instructStr =(String) args[0];//操作指令
        String ip = (String) args[1];//客户端ip

        String deviceCode = (String) args[2];//设备编码
        String operationType = (String) args[3];//操作类型——拉闸或者合闸等
        //添加日志记录
        IotOperationLog log = new IotOperationLog();
        log.setOperationContext(instructStr);
        log.setOperationType(InstructTypeConstant.getByKey(operationType).getValue());
        log.setOperationUser("admin");//谁操作的设置成谁
        log.setDeviceCode(deviceCode);
        log.setSendIp(ip);
        operationLogMapper.insert(log);

    }
}
